var express = require('express');
var router = express.Router();
var service = require('../service/service.js');

router.get('/index',function(q,s,n){
  var content = service.get_index_data();
  s.send(content);
});

router.get('/book/:id',async function(q,s,n){
  var id = q.params.id;
  var data=await service.get_book_data(id);
  var reviews= await service.get_book_reviews(id);
  var recommend=await service.get_book_recommend(id);
  var book_data={};
  book_data.data=JSON.parse(data);
  book_data.reviews=JSON.parse(reviews);
  book_data.recommend=JSON.parse(recommend);
  s.send(JSON.stringify(book_data));
});

router.get('/get_chapter_id',async function(q,s,n){
  var id = q.query.id;
  var summary = await service.get_chapter_id(id);
  var chapter_id=JSON.parse(summary)[0]._id;
  var res = await service.get_chapter_data(chapter_id)
  s.send(res);
});

router.get('/chapter_data/id/:id',async function(q,s,n){
  var id = q.params.id;
  s.setHeader('Cache-Control', 'no-cache');
	if(!id){
	   id = "";
  }
  var res = await service.get_chapter_content_data(id);
  s.send("dsdsd");
});

module.exports = router;
